package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.SurveyQuestion;
import com.tanhua.dubbo.api.db.SurveyQuestionsApi;
import com.tanhua.dubbo.mapper.OptionVoMapper;
import com.tanhua.dubbo.mapper.QuestionVoMapper;
import com.tanhua.dubbo.mapper.SurveyQuestionsMapper;
import com.tanhua.vo.OptionVo;
import com.tanhua.vo.QuestionVo;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 调查问题api impl
 * 增删改查
 * @author Dzh
 * @date 2021/07/17 20:07:23
 */
@Service
public class SurveyQuestionsApiImpl implements SurveyQuestionsApi {

    @Autowired
    private SurveyQuestionsMapper surveyQuestionsMapper;
    @Autowired
    private OptionVoMapper optionVoMapper;
    @Autowired
    private QuestionVoMapper questionVoMapper;





    @Override
    public Page<SurveyQuestion> queryQuestions(Integer paperId,Integer page, Integer pageSize) {
        Page<SurveyQuestion> ipage = new Page<>(page,pageSize);
        QueryWrapper<SurveyQuestion> query =new QueryWrapper<>();
        query.lambda().eq(SurveyQuestion::getSid,paperId);
        return (Page<SurveyQuestion>) surveyQuestionsMapper.selectPage(ipage,query);
    }


    /**
     * @param questionVo
     * @Author Huo 根据qid修改问题
     * @date 2021/07/18 12:07:09
     */
    @Override
    public SurveyQuestion queryQuestionsById(Integer questionId) {
        return surveyQuestionsMapper.selectById(questionId);
    }

    /**
     * @author 钱柏羽
     * @date 2021/07/18
     */
    @Override
    public void saveQuestions(QuestionVo questionVo) {
        questionVoMapper.saveQuestion(questionVo.getQuestion(),questionVo.getSid());
        Integer qid = questionVoMapper.getNewQuestionId();
        char c=65;
        char d=1;
        List<OptionVo> options = questionVo.getOptions();
        for (OptionVo option : options) {
            Integer score = option.getScore();
            String newOption = option.getOption();
            optionVoMapper.saveOptionScoreByQid(qid,c,score);
            optionVoMapper.saveOptionByQid(qid,c,newOption);
            c+=d;
        }
        SurveyQuestion surveyQuestion = surveyQuestionsMapper.selectById(questionVo.getQid());
    }

    /**
     * @author 钱柏羽
     * @date 2021/07/18
     */
    @Override
    @Transactional
    public void update(QuestionVo questionVo) {
        Integer qid = questionVo.getQid();
        String question = questionVo.getQuestion();
        questionVoMapper.updateQuestionById(qid,question);
        char c=65;
        char d=1;
        List<OptionVo> options = questionVo.getOptions();
        for (OptionVo option : options) {
            Integer score = option.getScore();
            String newOption = option.getOption();
           optionVoMapper.updateScoreByQid(qid,c,score);
           optionVoMapper.updateOptionByQid(qid,c,newOption);
           c+=d;
        }
    }

    /**
     * @author 钱柏羽
     * @date 2021/07/18
     */
    @Override
    public QuestionVo findOptionsByQuestionId(Integer questionId) {
        List<OptionVo> optionVos = optionVoMapper.findOptionVoByQid(questionId);
        QuestionVo questionVo = questionVoMapper.findByQid(questionId);
        questionVo.setOptions(optionVos);
        return questionVo;
    }

    /**
     * @author 钱柏羽
     * @date 2021/07/18
     */
    @Override
    public void deleteQuestionById(Integer questionId) {
        questionVoMapper.deleteQuestionByQid(questionId);
        optionVoMapper.deleteOptionsByQid(questionId);
        optionVoMapper.deleteOptionsScoreByQid(questionId);
    }

}
